package com.jichangxiu.server.platform.action;

import com.jichangxiu.common.annotation.Post;
import com.jichangxiu.common.annotation.Rest;
import com.jichangxiu.common.entity.bo.Record;
import com.jichangxiu.common.properties.JcxProperties;
import com.jichangxiu.common.utils.FileUtils;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * PlatformFileAction:
 *
 * @author Jcx
 * @create 2023-04-12 14:53
 */
@Slf4j
@Rest("/platform/file")
@Tag(name = "平台-文件管理控制器")
public class PlatformFileAction {

    @Resource
    private JcxProperties jcxProperties;

    @PreAuthorize("isAuthenticated()")
    @Post(value = "/uploadImage", logEnabled = false, parameterTrimEnabled = false, parameterRepeatEnabled = false)
    public Record uploadImage(MultipartFile file) {
        // 验证文件大小 -> 验证后缀 -> 保存到硬盘 -> 地址返回给前端
        FileUtils.checkFileSize(file);
        FileUtils.checkSuffix(file.getOriginalFilename(), jcxProperties.getFileProperties().getImageSuffix());
        return FileUtils.saveFile(file, jcxProperties.getFileProperties().getImageFolder());
    }

    @PreAuthorize("isAuthenticated()")
    @Post(value = "/uploadVideo", logEnabled = false, parameterTrimEnabled = false, parameterRepeatEnabled = false)
    public Record uploadVideo(MultipartFile file) {
        // 验证文件大小 -> 验证后缀 -> 保存到硬盘 -> 地址返回给前端
        FileUtils.checkFileSize(file);
        FileUtils.checkSuffix(file.getOriginalFilename(), jcxProperties.getFileProperties().getVideoSuffix());
        return FileUtils.saveFile(file, jcxProperties.getFileProperties().getVideoFolder());
    }

    @PreAuthorize("isAuthenticated()")
    @Post(value = "/uploadAudio", logEnabled = false, parameterTrimEnabled = false, parameterRepeatEnabled = false)
    public Record uploadAudio(MultipartFile file) {
        // 验证文件大小 -> 验证后缀 -> 保存到硬盘 -> 地址返回给前端
        FileUtils.checkFileSize(file);
        FileUtils.checkSuffix(file.getOriginalFilename(), jcxProperties.getFileProperties().getAudioSuffix());
        return FileUtils.saveFile(file, jcxProperties.getFileProperties().getAudioFolder());
    }

    @PreAuthorize("isAuthenticated()")
    @Post(value = "/uploadApk", logEnabled = false, parameterTrimEnabled = false, parameterRepeatEnabled = false)
    public Record uploadApk(MultipartFile file) {
        // 验证文件大小 -> 验证后缀 -> 保存到硬盘 -> 地址返回给前端
        FileUtils.checkFileSize(file);
        FileUtils.checkSuffix(file.getOriginalFilename(), jcxProperties.getFileProperties().getApkSuffix());
        return FileUtils.saveFile(file, jcxProperties.getFileProperties().getApkFolder());
    }

    @PreAuthorize("isAuthenticated()")
    @Post(value = "/uploadFile", logEnabled = false, parameterTrimEnabled = false, parameterRepeatEnabled = false)
    public Record uploadFile(MultipartFile file) {
        // 验证文件大小 -> 验证后缀 -> 保存到硬盘 -> 地址返回给前端
        FileUtils.checkFileSize(file);
        FileUtils.checkSuffix(file.getOriginalFilename(), jcxProperties.getFileProperties().getFileSuffix());
        return FileUtils.saveFile(file, jcxProperties.getFileProperties().getFileFolder());
    }

    @PreAuthorize("isAuthenticated()")
    @Post(value = "/uploadFiles", logEnabled = false, parameterTrimEnabled = false, parameterRepeatEnabled = false)
    public List<Record> uploadFiles(List<MultipartFile> files) {
        List<Record> records = new ArrayList<>();
        for (MultipartFile file : files) {
            // 验证文件大小 -> 验证后缀 -> 保存到硬盘 -> 地址返回给前端
            FileUtils.checkFileSize(file);
            FileUtils.checkSuffix(file.getOriginalFilename(), jcxProperties.getFileProperties().getFileSuffix());
            records.add(FileUtils.saveFile(file, jcxProperties.getFileProperties().getFileFolder()));
        }
        return records;
    }

}
